package com.mf.basic.mapper;

import com.mf.basic.entity.DtoPermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by Luke on 2025/3/7 16:06.
 */
public interface MapperPermission extends BaseMapper<DtoPermission> {

    /**
     * 查询角色的权限
     * @param roleId 角色id
     * @return List<DtoPermission>
     */
    @Select("SELECT t2.*,(CASE WHEN t1.permission_id IS NULL THEN 0 ELSE 1 END) choose FROM(" +
            " SELECT role_id,permission_id FROM sys_role_permission WHERE role_id=#{roleId} " +
            ")t1 RIGHT JOIN sys_permission t2 ON t1.permission_id=t2.id ORDER BY t2.pid ASC,t2.show_order DESC")
    List<DtoPermission> listByRoleId(Long roleId);

    /**
     * 查询用户的权限
     * @param userId 用户id
     * @return List<DtoPermission>
     */
    @Select("SELECT * FROM sys_permission WHERE id IN(\n" +
            " SELECT DISTINCT(permission_id)permission_id FROM sys_role_permission WHERE role_id IN \n" +
            " (SELECT id FROM sys_role WHERE EXISTS(SELECT 1 FROM sys_user_role WHERE user_id=#{userId} AND sys_user_role.role_id=sys_role.id))\n" +
            " )")
    List<DtoPermission> listByUserId(Long userId);

}
